/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.security;

import java.util.Locale;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import org.primefaces.context.RequestContext;
import src.domain.security.UsersConfig;
import src.services.security.UsersConfigModel;
import src.util.Messager;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
@ManagedBean
@RequestScoped
public class UsersConfigBean {
    UsersConfig usersConfig;
    UsersConfigModel helper;
    
    
    public UsersConfigBean() {
      usersConfig = new UsersConfig();
      helper = new UsersConfigModel();
      init();
    }
    
    private void init(){
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext(); 
      //  externalContext.getSessionMap().remove("LoginBean");
        String lang = "en"    ;
         this.usersConfig.setThemes("south-street");
        HttpSession session = (HttpSession) externalContext.getSession(false);
        if (session!=null){
            if (session.getAttribute("userid")!=null){
            
            this.usersConfig.setUserId(Integer.parseInt(session.getAttribute("userid").toString()));
            this.usersConfig = helper.getById(this.usersConfig.getUserId());
            lang = (this.usersConfig.getLocale()!=null?this.usersConfig.getLocale():"en");
            }
        }
       
        
        
          
         FacesContext.getCurrentInstance().getViewRoot().setLocale( new Locale.Builder().setLanguage(lang).build());
          
          
                    
    }

    public UsersConfig getUsersConfig() {
        return usersConfig;
    }
    
    
    public void applyConfig(){
        RequestContext context = RequestContext.getCurrentInstance();
        boolean success;
        //usersConfig.setUserId(userId);
        success = helper.saveData(usersConfig);
        Messager.showSaveUpdateStatus(success);     
        context.addCallbackParam("success", success);  
    }
}
